﻿using BCCommon.Enums;
using BCDto.Common.UploadFileRecord;
using BCDto.UploadHandleComponent;
using BCService.Common.UploadFileRecord;
using BCService.WeChatComponent.WeChatMaterialService.WeChatMaterialService;

namespace BCService.UploadHandleComponent.VideoHandle
{
    /// <summary>
    /// 视频处理
    /// </summary>
    public class VideoHandle : IUploadHandle
    {
        private readonly IUploadFileRecordService uploadFileRecordService;
        private readonly IWeChatMaterialService weChatMaterialService;
        private UploadFileDto uploadFileDto;

        public VideoHandle(IUploadFileRecordService uploadFileRecordService, IWeChatMaterialService weChatMaterialService)
        {
            this.uploadFileRecordService = uploadFileRecordService;
            this.weChatMaterialService = weChatMaterialService;
        }

        public void CheckData(UploadFileDto uploadFileDto)
        {
            this.uploadFileDto = uploadFileDto;
            if (uploadFileDto.Usage == UploadUsageType.WeChat)
            {
                this.weChatMaterialService.WeChatMaterialCheckData(this.uploadFileDto);
            }
        }

        public UploadResponseDto Process(UploadFileDto uploadFileDto)
        {
            string medialId = string.Empty;
            if (uploadFileDto.Usage == UploadUsageType.WeChat)
            {
                switch (uploadFileDto.RemarkType)
                {
                    case UploadFileRemarkType.WeChatPermanentMaterial:
                        medialId = this.weChatMaterialService.UploadWeChatMedial(this.uploadFileDto, true);
                        break;
                    case UploadFileRemarkType.WeChatTemporaryMaterial:
                        medialId = this.weChatMaterialService.UploadWeChatMedial(this.uploadFileDto, false);
                        break;
                }
            }
            UploadFileRecordDto uploadFileRecordDto = new UploadFileRecordDto();
            uploadFileRecordDto.UploadFileRecordNo = this.uploadFileDto.FileName;
            uploadFileRecordDto.FileSize = this.uploadFileDto.ContentLength;
            uploadFileRecordDto.FilePath = this.uploadFileDto.DirectoryPath;
            uploadFileRecordDto.FileUrl = this.uploadFileDto.Src;
            uploadFileRecordDto.FileSuffix = this.uploadFileDto.FileSuffix;
            uploadFileRecordDto.FileUsage = this.uploadFileDto.Usage.ToString();
            uploadFileRecordDto.RemarkType = this.uploadFileDto.Usage.Equals("Self") ? "" : this.uploadFileDto.RemarkType.ToString();
            uploadFileRecordDto.WebsiteName = this.uploadFileDto.WebsiteName;
            uploadFileRecordDto.WebsiteFileDirectory = this.uploadFileDto.WebsiteFileDirectory;
            uploadFileRecordDto.WebsiteFullName = this.uploadFileDto.WebsiteFullName;
            this.uploadFileRecordService.Add(uploadFileRecordDto);
            return new UploadResponseDto()
            {
                Message = medialId
            };
        }
    }
}
